package user_local_ewallet

import (
	"context"
	"errors"

	"gitee.com/liuxuezhan/ar-platform/assets-api/pkg/xerrors"
	"gitee.com/liuxuezhan/ar-platform/base-common/headInfo"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/assetsClient/rpc"
	"github.com/jinzhu/copier"

	"gitee.com/liuxuezhan/ar-platform/assets-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/assets-api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserLocalEWalletInsertLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 用户提现页面--本地电子钱包--新建
func NewUserLocalEWalletInsertLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserLocalEWalletInsertLogic {
	return &UserLocalEWalletInsertLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserLocalEWalletInsertLogic) UserLocalEWalletInsert(req *types.UserLocalEwalletInsertRequest) (resp *types.UserLocalEwalletInsertResponse, err error) {
	request := rpc.UserLocalEwalletInsertRequest{}
	err = copier.Copy(&request, req)
	if err != nil {
		return nil, xerrors.ErrDataConvert
	}
	userId := headInfo.GetTokenUid(l.ctx)
	if userId == 0 {
		return &types.UserLocalEwalletInsertResponse{
			IsInsert: false,
		}, errors.New("user is not exist")
	}
	request.UserId = userId
	page, err := l.svcCtx.AssetsRpc.UserChannel.UserLocalEWalletsInsert(l.ctx, &request)
	response := types.UserLocalEwalletInsertResponse{}
	if err != nil {
		return &response, err
	}
	err = copier.Copy(&response, page)
	if err != nil {
		return nil, xerrors.ErrDataConvert
	}
	return &response, nil
}
